<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/6/8
 * Time: 16:11
 */

namespace app\admin\model;

use app\admin\common\Base;
use app\admin\validate\SeoLink;
use app\admin\validate\News as NewsValidate;
use think\Model;

class News extends Model {
    protected $insert = ['status','is_recommended','time','create_time','update_time'];
    protected $update = ['status','is_recommended','time','update_time'];

    public function add($data){
        //实例化验证器
        $validate = new NewsValidate();
        //验证器验证
        if (!$validate->check($data)){
            return ['code'=>'err','msg'=>$validate->getError()];
        }
        //处理数据
        $data = $this->handleData($data,'add');
        if ($data['code'] == 'suc') {
            //写入数据库记录
            $result = self::allowField(true)->create($data['data']);
            if ($result) {
                insert_system_log('新增新闻：' . $result->id);
                return ['code' => 'suc', 'msg' => '新增成功'];
            } else {
                return ['code' => 'err', 'msg' => '添加新闻失败，请重试'];
            }
        }
        return ['code' => $data['code'],'msg' => $data['msg']];
    }

    public function edit($id,$data){
        //实例化验证器
        $validate = new NewsValidate();
        //验证器验证
        if (!$validate->check($data)){
            return ['code'=>'err','msg'=>$validate->getError()];
        }
        //处理数据
        $data = $this->handleData($data,'edit',$id);
        if ($data['code'] == 'suc') {
            //更新数据库记录
            $result = self::save($data['data'], ['id' => $id]);
            if ($result) {
                insert_system_log('编辑新闻：' . $id);
                return ['code' => 'suc', 'msg' => '编辑成功'];
            } else {
                return ['code' => 'err', 'msg' => '编辑新闻失败，请重试'];
            }
        }
        return ['code' => $data['code'],'msg' => $data['msg']];
    }

    private function handleData($data,$action,$id=''){

        if (!empty($data['seo_link'])){
            //实例化自定义链接验证器
            $link_validate = new SeoLink();
            //实例化基类控制器
            $base_controller = new Base();
            if (!$link_validate->check($data)){
                //链接格式错误
                return ['code'=>'err','msg'=>$link_validate->getError()];
            }

            if ($action == 'add') {
                //新增操作
                $data['seo_link'] = $base_controller->handleSeoUrl($data['seo_link'], 'news', 'add');
            } else {
                //修改操作
                $data['seo_link'] = $base_controller->handleSeoUrl($data['seo_link'], 'news', 'edit',$id);
            }
        }else{
            $data['seo_link'] = null;
        }

        return ['code' => 'suc','data' => $data];
    }

    protected function setStatusAttr($value){
        return empty($value) ? 0 : 1;
    }

    protected function setIsRecommendedAttr($value){
        return empty($value) ? 0 : 1;
    }

    protected function setTimeAttr($value){
        return strtotime($value) < 0 ? time() : strtotime($value);
    }

    protected function setCreateTimeAttr(){
        return time();
    }

    protected function setUpdateTimeAttr(){
        return time();
    }
}